using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.Reflection;

namespace By1eEurope.BKS.Controls
{
    /// <summary>
    /// Offers tow template properties. a IExpressionAnaylser-Implementation decides which template is displayed.
    /// </summary>
    public class Trimming : WebControl
    {
        private string evalExpression;
        /// <summary>
        /// Input for the IExpressionAnalyser
        /// </summary>
        public string EvalExpression
        {
            set { this.evalExpression = value;  }
        }

        private string evalExpressionAnalyser;
        /// <summary>
        /// Fullqualified classname of an implementation of the IExpressionAnalyser interface that makes the decision
        /// </summary>
        public string EvalExpressionAnalyser
        {
            set { this.evalExpressionAnalyser = value; }
        }

        private IExpressionAnalyser analyser;
        internal IExpressionAnalyser Analyser
        {
            get
            {
                if (this.analyser == null)
                {
                    try
                    {
                        Assembly a = Assembly.GetExecutingAssembly();
                        Type t = a.GetType(this.evalExpressionAnalyser);
                        this.analyser = (IExpressionAnalyser)Activator.CreateInstance(t);
                    }
                    catch (Exception)
                    {
                        this.analyser = null;
                    }
                }
                return this.analyser;
            }
        }


        /// <summary>
        /// This template is shown if the expression is true
        /// </summary>
        private ITemplate contentTemplate;
        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(TrimmingContrainer))]
        public ITemplate ContentTemplate
        {
            get { return this.contentTemplate; }
            set { this.contentTemplate = value; }
        }

        /// <summary>
        /// This template is shown if the expression is false
        /// </summary>
        private ITemplate otherwiseTemplate;
        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(TrimmingContrainer))]
        public ITemplate OtherwiseTemplate
        {
            get { return this.otherwiseTemplate; }
            set { this.otherwiseTemplate = value; }
        }

        protected override void CreateChildControls()
        {
            if (this.contentTemplate != null && this.Analyser != null && this.Analyser.AnalyseExpression(this.evalExpression, this.Page.Request, this.Context))
            {
                TrimmingContrainer tc = new TrimmingContrainer(this);
                tc.ID = this.ID+"TrimmingContrainer";
                this.contentTemplate.InstantiateIn(tc);
                this.Controls.Add(tc);
            }
            else if (this.otherwiseTemplate != null)
            {
                TrimmingContrainer tc = new TrimmingContrainer(this);
                tc.ID = this.ID+"TrimmingContrainer";
                this.otherwiseTemplate.InstantiateIn(tc);
                this.Controls.Add(tc);
            }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        { }
        public override void RenderEndTag(HtmlTextWriter writer)
        { }

        protected override void OnLoad(EventArgs e)
        {
            this.EnsureChildControls();
            base.OnLoad(e);
        }

    }
}
